﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StructureMap;
using Commerce.Services;
using Commerce.Data;

namespace Commerce.CSK.Web
{
    public class Services
    {
        #region Service Access Methods

        public static ICatalogService GetCatalogService()
        {
            return StructureMap.ObjectFactory.GetInstance<ICatalogService>();
        }

        public static IOrderService GetOrderService()
        {
            return StructureMap.ObjectFactory.GetInstance<IOrderService>();
        }

        public static IPersonalizationService GetPersonalizationService()
        {
            return StructureMap.ObjectFactory.GetInstance<IPersonalizationService>();
        }
        public static IPaymentService GetPaymentService()
        {
            return StructureMap.ObjectFactory.GetInstance<IPaymentService>();
        }
        #endregion

        public IList<Category> GetCategories()
        {
            return GetCatalogService().GetCategories();
        }


        #region Order Helper Methods

        /// <summary>
        /// Returns the user's currently active cart (order)
        /// </summary>
        /// <returns></returns>
        public static Order GetCurrentOrder()
        {
            return GetOrderService().GetCurrentOrder(UserName);
        }

        /// <summary>
        /// Adds a product to the current cart (order)
        /// </summary>
        /// <param name="productToAdd">Product to add</param>
        public static void AddProductToCart(Product productToAdd)
        {
            //Get the order services as we'll use it more than once. 
            IOrderService service = GetOrderService();

            Order currentOrder = service.GetCurrentOrder(UserName);

            currentOrder.AddItem(productToAdd);

            //Make sure that the order and all items are saved! 
            service.SaveItems(currentOrder);

            //Register the event
            AddUserEvent(new UserEvent(
                UserName,
                null,
                null,
                productToAdd.ID,
                currentOrder.ID,
                UserBehavior.AddItemToBasket));

        }
        #endregion


        #region Payment Helper Methods

        #endregion

        #region Personalization Helper Methods
        public static string UserName
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string userName = Guid.NewGuid().ToString();

                if (context != null)
                {

                    //if not, do we know who they are?
                    if (context.User.Identity.IsAuthenticated)
                    {
                        userName = context.User.Identity.Name;

                        //do they have a cookie?
                    }
                    else if (context.Request.Cookies["shopper"] != null)
                    {
                        userName = context.Request.Cookies["shopper"].Value;

                    }
                }
                UserName = userName;
                return userName;
            }
            set
            {
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    context.Response.Cookies["shopper"].Value = value;
                    context.Response.Cookies["shopper"].Expires = DateTime.Now.AddMonths(12);
                }
            }
        }

        /// <summary>
        /// Adds a ViewCategory event to the personalization service
        /// </summary>
        /// <param name="categoryId">ID of the category being viewed</param>
        public static void CategoryViewed(int categoryId)
        {
            AddUserEvent(new UserEvent(
                UserName,
                HttpContext.Current.Request.UserHostAddress,
                categoryId,
                null,
                GetCurrentOrder().ID,
                UserBehavior.ViewCategory));
        }


        /// <summary>
        /// Adds a ViewProduct event to the personalization service.
        /// </summary>
        /// <param name="productId">ID of the product being viewed </param>
        public static void ProductViewed(int productId)
        {
            AddUserEvent(new UserEvent(
                UserName,
                HttpContext.Current.Request.UserHostAddress,
                productId,
                null,
                GetCurrentOrder().ID,
                UserBehavior.ViewProduct));
        }

        /// <summary>
        /// Adds a user event to the personalization service
        /// </summary>
        /// <param name="userEvent">New user event to add</param>
        public static void AddUserEvent(UserEvent userEvent)
        {
            IPersonalizationService svc = GetPersonalizationService();
            //Make sure that the IP is there. 
            userEvent.IP = HttpContext.Current.Request.UserHostAddress;
            //Save to the db. 
            svc.SaveUserEvent(userEvent);
        }

        #endregion

    }



    /// <summary>
    /// Utility class to return formatted navigation links
    /// </summary>
    public class Links
    {
        public static string GetCategoryViewLink(Category selectedCategory)
        {
            return "~/ViewCategory.aspx?c=" + selectedCategory.ID.ToString();
        }

        public static string GetProductViewLink(Product selectedProduct)
        {
            return "~/ViewProduct.aspx?p=" + selectedProduct.ID.ToString();
        }

        public static string GetProductImagePath(Product selectedProduct)
        {
            return "~/Content/ProductImages/" + selectedProduct.DefaultImagePath;
        }

        public static string GetCartViewLink()
        {
            return "~/ViewCart.aspx";
        }
        public static string GetStaticImagePath(string imageName)
        {
            return "~/Content/Images/" + imageName;
        }
    }
}
